﻿validator =
{
    ip: function(source, args) {
        var ipValue = args.Value;
        var default_errormessage = 'ip [e.g. 127.0.0.1]';
        
        var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
        var ipArray = ipValue.match(ipPattern);

        var isValid = true;

        do {
            if (ipArray == null) {
                isValid = false;
                break;
            }

            // ipArray[0] = 11.22.33.44
            // ipArray[1] = 11
            // ipArray[2] = 22
            // ipArray[3] = 33
            // ipArray[4] = 44
            for (var i = 1; i <= 4; i++) {
                var thisSegment = parseInt(ipArray[i], 10) || 0;
                if (thisSegment > 255) {
                    isValid = false;
                    break;
                }
            }
        } while (0);

        if (!isValid) {
            if (source.innerText.length == 0)
                source.innerText = default_errormessage;
        }

        args.IsValid = isValid;
    },

    id: function(source, args) {
        var idValue = args.Value;
        var default_errormessage = 'id [e.g. A123456789]';
        
        var tab = "ABCDEFGHJKLMNPQRSTUVXYWZIO"
        var A1 = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3);
        var A2 = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5);
        var Mx = new Array(9, 8, 7, 6, 5, 4, 3, 2, 1, 1);

        var isValid = true;

        do {
            if (idValue.length != 10) {
                isValid = false;
                break;
            }

            i = tab.indexOf(idValue.charAt(0));
            if (i == -1) {
                isValid = false;
                break;
            }
            sum = A1[i] + A2[i] * 9;

            for (i = 1; i < 10; i++) {
                v = parseInt(idValue.charAt(i));
                if (isNaN(v)) return false;
                sum = sum + v * Mx[i];
            }
            isValid = (sum % 10 == 0);

        } while (0);

        if (!isValid) {
            if (source.innerText.length == 0)
                source.innerText = default_errormessage;
        }
        
        args.IsValid = isValid;
    },

    email: function(source, args) {
        var emailValue = args.Value;
        var default_errormessage = 'email [e.g. a@b.c]';
        
        var isValid = true;

        do {
            if (!emailValue) {
                isValid = false;
                break;
            }

            var emailPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
            isValid = emailPattern.test(emailValue);

        } while (0);

        if (!isValid) {
            if (source.innerText.length == 0)
                source.innerText = default_errormessage;
        }
        
        args.IsValid = isValid;
    },

    url: function(source, args) {
        var urlValue = args.Value;
        var default_errormessage = 'url [e.g. www.google.com]';
        
        var isValid = true;

        do {
            if (!urlValue) {
                isValid = false;
                break;
            }

            var urlPattern = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
            isValid = urlPattern.test(urlValue);

        } while (0);

        if (!isValid) {
            if (source.innerText.length == 0)
                source.innerText = default_errormessage;
        }
        
        args.IsValid = isValid;
    }


};

